//
//  HeardView.m
//  Simei
//
//  Created by 王声远 on 16/5/4.
//  Copyright © 2016年 创美汇. All rights reserved.
//

#import "SYZNView.h"

@interface SYZNView()

@property (nonatomic,assign)CGRect currentRect;

@end

@implementation SYZNView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.rate = 0;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    CGFloat offset = self.currentRect.size.width / 3.6; //设置3.6 出来的弧度最像圆形
    CGPoint rectCenter =
    CGPointMake(self.currentRect.origin.x + self.currentRect.size.width / 2,
                self.currentRect.origin.y + self.currentRect.size.height / 2);
    
    // 8个控制点实际的偏移距离。 The real distance of 8 control points.
    CGFloat extra = (self.currentRect.size.width * 2 / 5.0) * _rate;
    
    CGPoint pointA = CGPointMake(rectCenter.x, self.currentRect.origin.y);
    CGPoint pointB = CGPointMake(
                                 rectCenter.x + self.currentRect.size.width / 2,
                                 rectCenter.y);
    CGPoint pointC = CGPointMake(
                                 rectCenter.x, rectCenter.y + self.currentRect.size.height / 2+extra * 6);
    CGPoint pointD = CGPointMake(self.currentRect.origin.x,
                                 rectCenter.y);
    
    
    CGPoint c1 = CGPointMake(pointA.x+self.currentRect.size.width/4.0, pointA.y);
    CGPoint c2 = CGPointMake(pointB.x, rectCenter.y-self.currentRect.size.width/4.0);
    
    CGPoint c3 = CGPointMake(pointB.x-offset*_rate-extra*_rate, pointB.y + offset+extra*5*_rate);
    
    CGPoint c4 = CGPointMake(pointC.x+self.currentRect.size.width/4.0+2*_rate, pointC.y);
    
    CGPoint c5 = CGPointMake(pointC.x - self.currentRect.size.width/4.0-2*_rate, pointC.y);
    
    CGPoint c6 = CGPointMake(pointD.x+offset*_rate+extra*_rate, pointD.y + offset+extra*5*_rate);
    
    CGPoint c7 = CGPointMake(pointD.x, pointD.y - offset);
    CGPoint c8 = CGPointMake(pointA.x - offset, pointA.y);
    
    // 更新界面
    UIBezierPath *ovalPath = [UIBezierPath bezierPath];
    [ovalPath moveToPoint:pointA];
    [ovalPath addCurveToPoint:pointB controlPoint1:c1 controlPoint2:c2];
    [ovalPath addCurveToPoint:pointC controlPoint1:c3 controlPoint2:c4];
    [ovalPath addCurveToPoint:pointD controlPoint1:c5 controlPoint2:c6];
    [ovalPath addCurveToPoint:pointA controlPoint1:c7 controlPoint2:c8];
    [ovalPath closePath];
    [ovalPath setLineCapStyle:kCGLineCapRound]; //线条拐角
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddPath(ctx, ovalPath.CGPath);
    CGContextSetFillColorWithColor(ctx, self.fillColor.CGColor);
    CGContextFillPath(ctx);
}

- (CGRect)currentRect {
    CGFloat r = MIN(selfHeight*(1-_rate)+_rate*30, selfHeight);
    _currentRect = CGRectMake((self.frame.size.width-r)*0.5, 0, r, r);
    return _currentRect;
}

- (void)setRate:(CGFloat)rate {
    if (rate > 1 || rate < 0) {
        return;
    }
    _rate = rate;
    [self setNeedsDisplay];
}

@end
